<?php

namespace App\Models;

use App\Enums\CostRuleClassify;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @method withShop($shopId)
 */
class CostRule extends Model
{
    use HasFactory;

    use SoftDeletes;

    protected $fillable = [
        'name', 'data', 'type', 'store_id',
        'table_type', 'assistant_type_id', 'classify',
    ];

    protected $casts = [
        'classify' => CostRuleClassify::class,
    ];

    public function getDataAttribute($value)
    {
        return unserialize($value);
    }

    public function setDataAttribute($value)
    {
        $this->attributes['data'] = serialize($value);
    }

    public function assistantType()
    {
        return $this->belongsTo(AssistantType::class);
    }

    public function tableTypes()
    {
        return $this->belongsToMany(TableType::class);
    }

    public function store()
    {
        return $this->belongsTo(Store::class);
    }

    public function scopeWithShop($query, $shopId)
    {
        return $query->where('shop_id', $shopId);
    }
}
